package at.fhooe.mcm.logic.dataexport;

import java.io.File;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.NotSerializableException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import javax.swing.JFileChooser;
import javax.swing.JFrame;

import at.fhooe.mcm.logic.core.Core;
import at.fhooe.mcm.logic.core.Service;
import at.fhooe.mcm.logic.event.Event;
import at.fhooe.mcm.logic.event.EventListener;
import at.fhooe.mcm.logic.event.EventManager;
import at.fhooe.mcm.logic.logging.ConsoleLoggingService;
import at.fhooe.mcm.logic.sensors.Sensor;
import at.fhooe.mcm.logic.sensors.SensorManager;

public class DataExportService implements Service, EventListener
{
	public static final String	LOGGING_SOURCE	= "DataExportService";
	private static final String	NAME			= "DataExportService";
	private Core				m_core;

	public static DataExportService getInstance(Core _core)
	{
		return (DataExportService) _core.getService(NAME);
	}
	
	@SuppressWarnings("unchecked")
	public void deserializeSensors()
	{
		ArrayList<Sensor> sensors = new ArrayList<Sensor>();

		FileInputStream fis = null;
		ObjectInputStream ois = null;

		JFileChooser fileChooser = new JFileChooser();

		fileChooser.setFileFilter(new javax.swing.filechooser.FileFilter()
		{
			@Override
			public String getDescription()
			{
				return "Sensoren (*.sen)";
			}

			@Override
			public boolean accept(File f)
			{
				return f.getName().toLowerCase().endsWith(".sen") || f.isDirectory();
			}
		});
		int returnVal = fileChooser.showOpenDialog(new JFrame());

		if (returnVal == JFileChooser.APPROVE_OPTION)
		{
			File file = fileChooser.getSelectedFile();

			try
			{
				fis = new FileInputStream(file);
				ois = new ObjectInputStream(fis);

				ArrayList<Sensor> temp = (ArrayList<Sensor>) ois.readObject();
				sensors.addAll(temp);
				ois.close();
				fis.close();
			}
			catch (FileNotFoundException _fnfe)
			{
				ConsoleLoggingService.getInstance(m_core).logDebug(LOGGING_SOURCE, _fnfe.getMessage());
			}
			catch (IOException _ioe)
			{
				ConsoleLoggingService.getInstance(m_core).logDebug(LOGGING_SOURCE, _ioe.getMessage());
			}
			catch (ClassNotFoundException _cnfe)
			{
				ConsoleLoggingService.getInstance(m_core).logDebug(LOGGING_SOURCE, _cnfe.getMessage());
			}
		}

		System.out.println("adding sensors");
		for (Sensor s : sensors)
		{
			System.out.println("adding sensor");
			s.initialize(m_core);
			SensorManager.getInstance(m_core).addEntity(s);
		}
		
		ConsoleLoggingService.getInstance(m_core).logInfo(LOGGING_SOURCE, "Data import completed.");
	}

	/**
	 * stores GeoObjects in a file
	 * 
	 * @param _geos
	 *            the GeoObjects to store
	 */
	public void serializeSensors()
	{
		// System.out.println("number of sensors to serialize:"+_sensors.size());
		FileOutputStream fos = null;
		ObjectOutputStream out = null;

		JFileChooser fileChooser = new JFileChooser();

		fileChooser.setFileFilter(new javax.swing.filechooser.FileFilter()
		{
			@Override
			public String getDescription()
			{
				return "Sensoren (*.sen)";
			}

			@Override
			public boolean accept(File f)
			{
				return f.getName().toLowerCase().endsWith(".sen") || f.isDirectory();
			}
		});

		int returnVal = fileChooser.showSaveDialog(new JFrame());

		if (returnVal == JFileChooser.APPROVE_OPTION)
		{
			File file = fileChooser.getSelectedFile();

			if (null == file)
			{
				return;
			}

			try
			{
				fos = new FileOutputStream(file);
				out = new ObjectOutputStream(fos);
				out.writeObject(SensorManager.getInstance(m_core).getAllSensors());
				out.close();
				fos.close();
			}
			catch (FileNotFoundException _fnfe)
			{
				ConsoleLoggingService.getInstance(m_core).logDebug(LOGGING_SOURCE, _fnfe.getMessage());
			}
			catch (NotSerializableException _nse)
			{
				ConsoleLoggingService.getInstance(m_core).logDebug(LOGGING_SOURCE, _nse.getMessage());
			}
			catch (IOException _ioe)
			{
				ConsoleLoggingService.getInstance(m_core).logDebug(LOGGING_SOURCE, _ioe.getMessage());
			}

			ConsoleLoggingService.getInstance(m_core).logInfo(LOGGING_SOURCE, "Data export completed.");
		}
	}

	@Override
	public String getName()
	{
		return NAME;
	}

	@Override
	public void initialize(Core core)
	{
		m_core = core;
		EventManager.getInstance(m_core).addListener(this, SensorSerializationEvent.TYPE);
	}

	@Override
	public void handleEvent(Event event)
	{
		if(event.isOfType(SensorSerializationEvent.TYPE))
		{
			SensorSerializationEvent sse = (SensorSerializationEvent) event;
			
			switch (sse.getMode())
			{
			case SERIALIZE:
				serializeSensors();
				break;
			case DESERIALIZE:
				deserializeSensors();
				break;
			default:
				break;
			}
		}
		
	}

	@Override
	public void shutdown(Core core)
	{
		// TODO Auto-generated method stub
		
	}

}
